﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win.CSkin.Test
{
    public partial class FrmProgress : Form
    {
        private BackgroundWorker bgWorker = new BackgroundWorker();

        public FrmProgress()
        {
            InitializeComponent();

            InitializeBackgroundWorker();
        }

        /// <summary>
        /// BackgroundWorker设置
        /// </summary>
        private void InitializeBackgroundWorker()
        {
            bgWorker.WorkerReportsProgress = true;
            bgWorker.WorkerSupportsCancellation = true;
            //异步执行代码
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            //当调用BackgroundWorker.ReportProgress(int percentProgress)方式时触发该事件
            bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgessChanged);
            //异步操作完成或取消时执行的操作，当调用DoWork事件执行完成时触发。
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_WorkerCompleted);
        }

        public void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i <= 100; i++)
            {
                if (bgWorker.CancellationPending) //是否取消异步操作
                {
                    e.Cancel = true;
                    return;
                }
                else
                {
                    //向ProgressChanged报告进度
                    bgWorker.ReportProgress(i, "Working");
                    System.Threading.Thread.Sleep(10);
                }
            }
        }

        public void bgWorker_ProgessChanged(object sender, ProgressChangedEventArgs e)
        {
            //string state = (string)e.UserState;//接收ReportProgress方法传递过来的userState
            this.progressBar1.Value = e.ProgressPercentage;
            this.label1.Text = "处理进度:" + Convert.ToString(e.ProgressPercentage) + "%";
        }

        public void bgWorker_WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.ToString());
                return;
            }
            if (!e.Cancelled)
                this.label1.Text = "处理完毕!";
            else
                this.label1.Text = "处理终止!";

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            //防止重复执行异步操作引发错误
            if (bgWorker.IsBusy)
                return;

            this.progressBar1.Maximum = 100;
            this.btnStart.Enabled = false;
            this.btnStop.Enabled = true;
            //启动异步操作
            bgWorker.RunWorkerAsync("hello");
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            this.btnStart.Enabled = true;
            this.btnStop.Enabled = false;
            //执行取消操作
            bgWorker.CancelAsync();
        }
    }
}
